﻿namespace NJQuery.Configuration {
	using System.Configuration;
	using System.Diagnostics.CodeAnalysis;

	/// <summary>
	/// Represents a collection of <see cref="ClientScript"/> objects.
	/// This class cannot be inherited.
	/// </summary>
	[SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface", Justification = "If the framework didn't implement the generic interface, I'm not going to.")]
	public sealed class ClientScriptCollection : ConfigurationElementCollection {


		#region Constructors

		/// <summary>
		/// This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
		/// Initializes a new instance of the <see cref="ClientScriptCollection"/> class.
		/// </summary>
		public ClientScriptCollection() { }

		#endregion


		#region Indexers

		/// <summary>
		/// Gets or sets the <see cref="ClientScript"/> at a particular position.
		/// </summary>
		/// <param name="index">The zero-based index to get or set.</param>
		/// <returns>The <see cref="ClientScript"/> at the index specified by <paramref name="index"/>.</returns>
		public ClientScript this[int index] {
			get { return BaseGet(index) as ClientScript; }
			set {
				if(base.BaseGet(index) != null) BaseRemoveAt(index);
				BaseAdd(index, value);
			}
		}

		/// <summary>
		/// Gets a <see cref="ClientScript"/> in the collection with a matching <see cref="ClientScript.Name"/> property value.
		/// </summary>
		/// <param name="name">The invariant script name to look up.</param>
		/// <returns>The first <see cref="ClientScript"/> in the collection matching the provided <paramref name="name"/>.  A null value is returned if no matches are found.</returns>
		public new ClientScript this[string name] {
			get {
				foreach(ClientScript script in this)
					if(script.Name == name) return script;
				return null;
			}
		}

		#endregion


		#region Methods: Public

		/// <summary>
		/// Returns whether or not the collection contains a script with a matching <see cref="ClientScript.Name"/> property value.
		/// </summary>
		/// <param name="name">The invariant script name to look up.</param>
		/// <returns>Whether or not a matching <paramref name="name"/> was found.</returns>
		public bool Contains(string name) {
			return this[name] != null;
		}

		/// <summary>
		/// Adds a <see cref="ClientScript"/> object to the <see cref="ClientScriptCollection"/> instance.
		/// </summary>
		/// <param name="script">The <see cref="ClientScript"/> object to add.</param>
		public void Add(ClientScript script) {
			BaseAdd(script);
		}

		/// <summary>
		/// Removes all <see cref="ClientScript"/> objects from a <see cref="ClientScriptCollection"/> object.
		/// </summary>
		public void Clear() {
			BaseClear();
		}

		/// <summary>
		/// Removes the specified <see cref="ClientScript"/> from the <see cref="ClientScriptCollection"/> object.
		/// </summary>
		/// <param name="script">The <see cref="ClientScript"/> object to remove.</param>
		public void Remove(ClientScript script) {
			if(BaseIndexOf(script) >= 0)
				BaseRemove(script.Name);
		}

		#endregion


		#region Methods: Protected Overrides

		/// <summary>
		/// Creates a new <see cref="ConfigurationElement"/> object.
		/// </summary>
		/// <returns>A new <see cref="ConfigurationElement"/> object.</returns>
		protected override ConfigurationElement CreateNewElement() {
			return new ClientScript();
		}

		/// <summary>
		/// Gets the element key for the specified <see cref="ConfigurationElement"/> object.
		/// </summary>
		/// <param name="element">The <see cref="ConfigurationElement"/> object whose key you want to return.</param>
		/// <returns>The object acts as the key for the specified <see cref="ConfigurationElement"/> instance.</returns>
		protected override object GetElementKey(ConfigurationElement element) {
			return ((ClientScript)element).Name;
		}

		#endregion


	}

}